/*
 * StringUtilities.java
 *    Author: nfk / dru
 *      Date: Sep 19, 2008
 *      
 *   Classes:
 *      StringUtilities
 */

package at.syncme.framework.legacy;


import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Vector;


/**
 * A string utility class containing e.g. a string tokenize method and a newline reader on an input stream.
 * @author nfk / dru
 */
public final class StringUtilities {

    /**
     * Tokenizes the specified string, with a space as delimiter.
     * @param s the string to tokenize
     * @return the array of tokens
     * @throws NullPointerException if s is null
     */
    public static String[] tokenize(final String s) {
        return StringUtilities.tokenize(s, ' ');
    }


    /**
     * Tokenizes the specified string, with given char as delimiter.
     * @param s the string to tokenize
     * @param delimiter the delimiter
     * @return the array of tokens
     * @throws NullPointerException if s is null
     */
    public static String[] tokenize(final String s, final char delimiter) {
        if (s == null) {
            throw new NullPointerException("s must not be null.");
        }

        final Vector tokens = new Vector();
        final StringBuffer buffer = new StringBuffer();
        int i = 0;
        char nextChar;

        while (i < s.length()) {
            nextChar = s.charAt(i);

            if (nextChar == delimiter) {
                tokens.addElement(buffer.toString().trim());
                buffer.setLength(0);
            } else {
                buffer.append(nextChar);
            }

            i++;
        }

        if (buffer.length() > 0) {
            tokens.addElement(buffer.toString().trim());
        }

        final String[] tokenArray = new String[tokens.size()];
        tokens.copyInto(tokenArray);

        return tokenArray;
    }


    /**
     * Transforms an enumeration into a String[] array.
     * @param enumeration the enumeration to transform
     * @return String[] containing the toString() of the enumeration's objects
     * @throws NullPointerException if enumeration is null
     */
    public static String[] transformEnumeration(final Enumeration enumeration) {
        final Vector v = new Vector();

        while (enumeration.hasMoreElements()) {
            v.addElement(String.valueOf(enumeration.nextElement()));
        }

        final String[] result = new String[v.size()];
        v.copyInto(result);

        return result;
    }


    /**
     * Grabs the next line from the input stream and returns it as a string.
     * @param is the input stream
     * @return the next line
     * @throws NullPointerException if is is null
     * @throws IOException if any network error occurs
     */
    public static String readLine(final InputStream is) throws IOException {
        if (is == null) {
            throw new NullPointerException("is must not be null.");
        }

        final StringBuffer buffer = new StringBuffer();
        int c = -1;
                
        do {
            c = is.read();
            buffer.append((char) c);
        } while (c != '\n' && c != -1);
        
        if (c == -1) {
            throw new IOException("input stream dead");
        }

        return buffer.toString();
    }
    
    /**
     * read a line as bytes
     * 
     * @param is
     * @return
     * @throws IOException
     */
    public static byte[] readLineAsBytes(final InputStream is) throws IOException {
        byte[] buffer = new byte[255];
        
        int length = 0, c;
        do {
            c = is.read(buffer);
            buffer[length++] = (byte)c;
        } while (c != '\n' && c != -1);
        
        if (c == -1) {
            throw new IOException("input stream dead");
        }
        
        byte[] result = new byte[length];
        System.arraycopy(buffer, 0, result, 0, length);
        return result;
    }


    /**
     * Converts a character to an integer value.
     * @param ch the character to convert
     * @return integer value of the character
     */
    public static int getNumericValue(final char ch) {
        return (ch - '0');
    }


    /** Forbid construction on utility class. */
    private StringUtilities() {
        throw new Error();
    }

}

/* End of StringUtilities.java */
